﻿
namespace Share
{
    public class SingletonObject<T> where T : class, new()
    {
        private static T _instance;
        private static readonly object _lock = new object(); // 线程安全锁
        bool m_bInited = false;
        public void DoInit()
        {
            if (Inited) return;
            OnInit();
            Inited = true;
        }

        protected bool Inited
        {
            get
            {
                return m_bInited;
            }
            set
            {
                m_bInited = value;
            }
        }

        protected virtual void OnInit()
        {
        }
        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock) // 避免多线程重复创建
                    {
                        _instance ??= new T();
                        (_instance as SingletonObject<T>).DoInit();
                    }
                }
                return _instance;
            }
        }

        public virtual string Serialization()
        {
            return "";
        }
        public virtual void UnSerialization(string jsonText)
        {
        }
    }
}